<?php
class UinformationsController extends AppController {

	var $name = 'Uinformations';
        
	var $uses = array('Uinformation','Child','Otherservice','Publication',
            'Deputation','Award','Deptexam','Education','Language','Precord',
            'Promotion','Recreation','Seniorscalexam','Service','Tinformation',
            'Travel','User');
 var $components = array('Upload','RequestHandler');
	
	function beforeFilter() {
    parent::BeforeFilter();   	
	//$this->Auth->allow('login','logout','userlist','add','confirmaccount');
	$this->Auth->allow('vpdf');
}

function vpdf(){
    Configure::write('debug',0);
     $this->layout = 'pdf'; //this will use the pdf.ctp layout
        $this->render(); 
}

function ascdate($id){
$this->layout='';
$this->autoRender=FALSE;
$fstdate= $this->Precord->find('all',array('conditions'
                    => array('Precord.user_id' => $id),
	'order' => array('Precord.datefrom ASC')
	));
return $fstdate;
}


function dscdate($id){
$this->layout='';
$this->autoRender=FALSE;
$curdate= $this->Precord->find('all',array('conditions'
                    => array('Precord.user_id' => $id),
	'order' => array('Precord.datefrom DESC')
	));
return $curdate;
}

function index() {
		$this->Uinformation->recursive = 0;
		$this->set('uinformations', $this->paginate());
	}

	function view($id=null) {
		/*if (!$id) {
			$this->Session->setFlash(__('Invalid uinformation', true));
			$this->redirect(array('action' => 'index'));
		}*/
		if($id==null)
		$id=$this->Auth->user('id');
		
		
		$this->set('uinformation', $this->Uinformation->find("user_id=$id"));
		
		$this->set('children', $this->Child->findAllByUserId($id));

                $this->set('otherservices', $this->Otherservice->findAllByUserId($id));

                 $this->set('publications', $this->Publication->findAllByUserId($id));

                 $this->set('deputations', $this->Deputation->findAllByUserId($id));

                  $this->set('awards', $this->Award->findAllByUserId($id));
		
	}

        function fullview($id=null) {
		/*if (!$id) {
			$this->Session->setFlash(__('Invalid uinformation', true));
			$this->redirect(array('action' => 'index'));
		}*/
		if($id==null)
		$id=$this->Auth->user('id');

		$this->set('uinformation', $this->Uinformation->find("user_id=$id"));
		$this->set('children', $this->Child->findAllByUserId($id));
                $this->set('otherservices', $this->Otherservice->findAllByUserId($id));
                $this->set('publications', $this->Publication->findAllByUserId($id));
                $this->set('deputations', $this->Deputation->findAllByUserId($id));
                $this->set('awards', $this->Award->findAllByUserId($id));
                $this->set('educations',$this->Education->findAllByUserId($id));
		$this->set('languages', $this->Language->findAllByUserId($id));
		$this->set('deptexams', $this->Deptexam->findAllByUserId($id));
		$this->set('seniorscalexams', $this->Seniorscalexam->findAllByUserId($id));
                $this->set('services', $this->Service->findAllByUserId($id));
		$this->set('promotions', $this->Promotion->findAllByUserId($id));
		$this->set('tinformations', $this->Tinformation->find('all',array('conditions'
                    => array('Tinformation.user_id' => $id),
	'order' => array('Tinformation.flag DESC')
	)));
		$this->set('precords', $this->Precord->findAllByUserId($id));
		$this->set('travels', $this->Travel->findAllByUserId($id));
		$this->set('recreations',  $this->Recreation->findAllByUserId($id));

	}

          function fullviewprint($id=null) {
              $this->layout='';
		/*if (!$id) {
			$this->Session->setFlash(__('Invalid uinformation', true));
			$this->redirect(array('action' => 'index'));
		}*/
		if($id==null)
		$id=$this->Auth->user('id');

		$this->set('uinformation', $this->Uinformation->find("user_id=$id"));
		$this->set('children', $this->Child->findAllByUserId($id));
                $this->set('otherservices', $this->Otherservice->findAllByUserId($id));
                $this->set('publications', $this->Publication->findAllByUserId($id));
                $this->set('deputations', $this->Deputation->findAllByUserId($id));
                $this->set('awards', $this->Award->findAllByUserId($id));
                $this->set('educations',$this->Education->findAllByUserId($id));
		$this->set('languages', $this->Language->findAllByUserId($id));
		$this->set('deptexams', $this->Deptexam->findAllByUserId($id));
		$this->set('seniorscalexams', $this->Seniorscalexam->findAllByUserId($id));
                $this->set('services', $this->Service->findAllByUserId($id));
		$this->set('promotions', $this->Promotion->findAllByUserId($id));
		$this->set('tinformations', $this->Tinformation->find('all',array('conditions'
                    => array('Tinformation.user_id' => $id),
	'order' => array('Tinformation.flag DESC')
	)));
		$this->set('precords', $this->Precord->findAllByUserId($id));
		$this->set('travels', $this->Travel->findAllByUserId($id));
		$this->set('recreations',  $this->Recreation->findAllByUserId($id));

	}


	function add() {
		if (!empty($this->data)) {

                if(strlen($this->data['Uinformation']['photo']['name'])>0)
		{
	$gear_image = $this->data['Uinformation']['photo'];
	$uploadFolder = 'img'.DS.'photo'.DS;
        $fileName = rand()."_".$gear_image['name'];

        $result = $this->Upload->upload($gear_image, $uploadFolder, $fileName, array('type' => 'resizecrop', 'size' => array(300,200), null));
		$this->data['Uinformation']['photo']=$fileName;
		}else{
                   $this->data['Uinformation']['photo']= null;
                }

//                echo 'hello';
//                exit;

		$this->data['Uinformation']['user_id']=$this->Auth->user('id');
			$this->Uinformation->create();
			if ($this->Uinformation->save($this->data)) {
				$this->Session->setFlash(__('The Personal information has been saved', true));
				$this->redirect(array('action' => 'view'));
			} else {
				$this->Session->setFlash(__('The Personal information could not be saved. Please, try again.', true));
			}
		}
		$users = $this->Uinformation->User->find('list');
		$this->set(compact('users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid uinformation', true));
			$this->redirect(array('action' => 'view'));
		}
		if (!empty($this->data)) {
               if(strlen($this->data['Uinformation']['photo']['name'])>0)
		{
	$gear_image = $this->data['Uinformation']['photo'];
	$uploadFolder = 'img'.DS.'photo'.DS;
        $fileName = rand()."_".$gear_image['name'];

        $result = $this->Upload->upload($gear_image, $uploadFolder, $fileName, array('type' => 'resizecrop', 'size' => array(300,200), null));
		$this->data['Uinformation']['photo']=$fileName;
		}
                else{
                   $this->data['Uinformation']['photo']= $this->data['Uinformation']['prephoto'];
                }
			if ($this->Uinformation->save($this->data)) {
				$this->Session->setFlash(__('The Personal information has been saved', true));
				$this->redirect(array('action' => 'view'));
			} else {
				$this->Session->setFlash(__('The Personal information could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Uinformation->read(null, $id);
			// print_r($this->data);
			// echo "sdfsdf";
			// exit;
		}
		$users = $this->Uinformation->User->find('list');
		$this->set(compact('users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Personal information', true));
			$this->redirect(array('action'=>'view'));
		}
		if ($this->Uinformation->delete($id)) {
			$this->Session->setFlash(__('Personal information deleted', true));
			$this->redirect(array('action'=>'view'));
		}
		$this->Session->setFlash(__('Personal information was not deleted', true));
		$this->redirect(array('action' => 'view'));
	}
}
?>